package com.lxr.test5002.utils;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.lxr.test5002.R;

/**
 * Created by uniriho on 2017/4/14.
 */

public class ToastUtils {
    private static Toast mToast = null;
    private static String oldMsg;
    private static long time;

    public static void showMessage(Context context, String msg) {
        stopMessage();
        View view = LayoutInflater.from(context).inflate(R.layout.layout_toast, null);
        TextView title = (TextView) view.findViewById(R.id.tv_toast_message);
        title.setText(msg);
        mToast = new Toast(context);
        mToast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER, 0, 70);
        mToast.setDuration(Toast.LENGTH_SHORT);
        mToast.setView(view);
        if (!msg.equals(oldMsg)) {
            mToast.show();
            time = System.currentTimeMillis();
        } else {
            if ((System.currentTimeMillis() - time) > 2000) {
                mToast.show();
                time = System.currentTimeMillis();
            }
        }
        oldMsg = msg;
    }

    public static void stopMessage() {
        if (mToast != null) {
            mToast.cancel();
            mToast = null;
        }
    }

}
